package org.wendu.wdoa.api;


import org.wendu.wdoa.common.CurrentUser;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.service.PasswordService;
import org.wendu.wdoa.model.User;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Validated
@RestController
@RequestMapping("/security/pwd")
public class PasswordController {

    @Resource
    private PasswordService passwordService;

    @PutMapping("")   //@AuthenticationPrincipal 说明参数是SpringSecurity当前用户
    public Result changeUserPwd(@AuthenticationPrincipal CurrentUser currentUser,User user){
        user.setU_id(currentUser.getUserId());
        user.setU_name(currentUser.getUserName());
        //判断密码是否正确
        int rightPassword = passwordService.isRightPassword(user);
        if (rightPassword == 1){

            //密码正确，修改密码操作
            passwordService.updateUserPwd(user);

            return Result.OK("密码修改成功");
        }else {
            return Result.err(200,"密码错误");
        }

    }





}
